package com.hsytech.baoinbao;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.EditText;


/**
 * Created by hsy on 2016/3/18.
 */
public class EditTextExt extends EditText {
    private OnTextChangedListener mOnTextChangedListener;
    private TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            Log.e("yyy", "beforeTextChanged s=" + EditTextExt.this.getText().toString());
            if (null != mOnTextChangedListener) {
                mOnTextChangedListener.beforeTextChanged(EditTextExt.this, s, start, count, after);
            }
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Log.e("yyy", "onTextChanged s=" + EditTextExt.this.getText().toString());
            if (null != mOnTextChangedListener) {
                mOnTextChangedListener.onTextChanged(EditTextExt.this, s, start, count);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
            Log.e("yyy", "afterTextChanged s=" + EditTextExt.this.getText().toString());
            if (null != mOnTextChangedListener) {
                mOnTextChangedListener.afterTextChanged(EditTextExt.this, s);
            }
        }
    };

    public EditTextExt(Context context) {
        super(context);
        init(context);
    }

    public EditTextExt(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public EditTextExt(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        super.addTextChangedListener(mTextWatcher);
    }

    public void setOnTextChangedListener(OnTextChangedListener listener) {
        this.mOnTextChangedListener = listener;
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
    }

    public static interface OnTextChangedListener {
        void beforeTextChanged(EditTextExt editTextExt, CharSequence s, int start, int count, int after);

        void afterTextChanged(EditTextExt editTextExt, Editable s);

        void onTextChanged(EditTextExt editTextExt, CharSequence s, int start, int count);
    }
}
